package org.wmq.skyserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wmq.skycommon.constant.PasswordConstant;
import org.wmq.skycommon.constant.StatusConstant;
import org.wmq.skycommon.context.BaseContext;
import org.wmq.skycommon.pojo.entity.Employee;
import org.wmq.skycommon.pojo.DTO.EmployeeDTO;
import org.wmq.skycommon.pojo.DTO.EmployeePageQueryDTO;
import org.wmq.skycommon.pojo.entity.PageResult;
import org.wmq.skycommon.utils.Md5Util;
import org.wmq.skyserver.mapper.EmployeeMapper;
import org.wmq.skyserver.service.EmployeeService;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PageHelperProperties pageHelperProperties;

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        String password = Md5Util.getMd5(PasswordConstant.DEFAULT_PASSWORD);
        employee.setPassword(password);
        employee.setStatus(StatusConstant.ENABLE);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        this.employeeMapper.insert(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        Page<Employee> page = this.employeeMapper.pageQuery(employeePageQueryDTO.getName());
        return new PageResult((Long)page.getTotal(),page.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(status);
        this.employeeMapper.update(employee);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = this.employeeMapper.selectById(id);
        employee.setPassword("********");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());
        this.employeeMapper.update(employee);
    }
}
